// pages/addStore/index.js
import { search } from '../../apis/api/index';
// import { SERVER_BASE } from '../../services/env'
const SERVER_BASE = ''
const app = getApp()
Page({
  data: {
    storeAddress: '',
    contactNumber: '',
    storeName: '',
    operatorName: '',
    managerMobileNum: '',
    longitude: '',
    latitude: '',
    warehouseAddress: ''
  },
  handleOpenMap(e) {
    const {type} = e.currentTarget.dataset
    const _this = this
    wx.chooseLocation({
      success: (res) => {
        const { address, latitude = 0, longitude = 0, name } = res
        console.log('腾讯地图', res)
        if (type === 'store') {
          _this.setData({
            storeAddress: address + '----' + name
          })
        }
      },
      fail: (err) => {
        console.log(err)
        if (err.errMsg === 'chooseLocation:fail cancel') return
        app.showToast('打开地图失败')
      }
    })
  },
  validatePhone(number) {
    const reg = /^(?:(?:\+|00)86)?1\d{10}$/
    return reg.test(number)
  },
  validateTEL(number) {
    const reg = /^(?:(?:\d{3}-)?\d{8}|^(?:\d{4}-)?\d{7,8})(?:-\d+)?$/
    return reg.test(number)
  },
  async submit() {
    const {
      contactNumber,
      managerMobileNum,
    } = this.data
  
    if (flag) {
      app.showToast('请输入有效地址数据')
      return
    }
    if (!!contactNumber && !this.validatePhone(contactNumber) && !this.validateTEL(contactNumber)) {
      app.showToast('请输入正确的号码')
      return
    }
    if (!!managerMobileNum && !this.validatePhone(managerMobileNum)) {
      app.showToast('管理员手机号不正确')
      return
    }
    const storeId = await insertStore()
    app.showToast('新增成功！')
    setTimeout(() => {
      wx.navigateTo({
        url: `/pages/qrCodePage/index?storeId=${storeId}`,
      })
    }, 2000)
  },
  onLoad() {
    const { name } = app.globalData.user
    this.setData({
      operatorName: name
    })
  }
})
